const baseUtils = {

  /**
     * 格式化时间戳
     * @param {*} timeStamp
     * @returns
     */
  formatDate(timeStamp) {
    const t = new Date(timeStamp)
    const year = t.getFullYear()
    const month = t.getMonth() + 1
    const date = t.getDate()
    const hour = t.getHours()
    const minute = t.getMinutes()
    if (minute < 10) {
      return `${year}-${month}-${date} ${hour}:0${minute}`
    } else {
      return `${year}-${month}-${date} ${hour}:${minute}`
    }
  },

  formatDate2(timeStamp) {
    const t = new Date(timeStamp)
    const year = t.getFullYear()
    const month = t.getMonth() + 1
    const date = t.getDate()
    return `${year}-${month}-${date}`
  },
  formatDate3(timeStamp) {
    const t = new Date(timeStamp)
    const year = t.getFullYear()
    const month = t.getMonth() + 1
    const date = t.getDate()
    const hour = t.getHours()
    const minute = t.getMinutes()
    if (minute < 10) {
      return `${year} 年 ${month} 月 ${date} 日${hour}:0${minute}`
    } else {
      return `${year} 年 ${month} 月 ${date} 日 ${hour}:${minute}`
    }
  },
  formatDate4(timeStamp) {
    const t = new Date(timeStamp)
    const year = t.getFullYear()
    const month = t.getMonth() + 1
    const date = t.getDate()
    return `${year} 年 ${month} 月 ${date} 日`
  },
  // 计算时间差
  handleDate(dateTimeStamp) {
    const minute = 1000 * 60
    const hour = minute * 60
    const day = hour * 24
    let result = ''
    const now = new Date().getTime()
    const diffValue = now - dateTimeStamp

    if (diffValue < 0) {
      console.log('时间不对劲,服务器创建时间与当前时间不同步')
      result = '刚刚'
    }
    const dayC = diffValue / day
    const hourC = diffValue / hour
    const minC = diffValue / minute

    if (parseInt(dayC) > 3) {
      result = '' + this.formatDate2(dateTimeStamp, 'yyyy-MM-DD')
    } else if (parseInt(dayC) > 1) {
      result = '' + parseInt(dayC) + '天前'
    } else if (parseInt(dayC) === 1) {
      result = '昨天'
    } else if (hourC >= 1) {
      result = '' + parseInt(hourC) + '小时前'
    } else if (minC >= 5) {
      result = '' + parseInt(minC) + '分钟前'
    } else { result = '刚刚' }
    return result
  },

  allegeDevice() {
    let equipmentType = ''
    const agent = navigator.userAgent.toLowerCase()
    const android = agent.indexOf('android')
    const iphone = agent.indexOf('iphone')
    const ipad = agent.indexOf('ipad')
    if (android !== -1) {
      equipmentType = 'android'
    }
    if (iphone !== -1 || ipad !== -1) {
      equipmentType = 'ios'
    }
    return equipmentType
  },

  /**
   * 判断变量是否为空  不包括fun 和 Date
   * @param {*} value
   * @returns
   */
  isEmpty(value) {
    if (Object.prototype.toString.call(value) === '[object Null]') {
      return true
    }

    if (Object.prototype.toString.call(value) === '[object Date]') {
      return false
    }

    if (Object.prototype.toString.call(value) === '[object Function]') {
      return false
    }

    if (Object.prototype.toString.call(value) === '[object Object]') {
      value = JSON.stringify(value)
      if (value === '{}') {
        return true
      }
    }
    if (Object.prototype.toString.call(value) === '[object Array]') {
      if (value.length === 0) {
        return true
      }
    }
    if (Object.prototype.toString.call(value) === '[object String]') {
      if (value === '' || value === '{}') {
        return true
      }
    }
    if (Object.prototype.toString.call(value) === '[object Number]') {
      if (value === 0) {
        return true
      }
      return false
    }
  }
}

export { baseUtils }
